#!/bin/bash


# --------------升级open ssl --------
function upgradeOpenSSL(){
    # install openssl 1.0.2
    color blue "begin install openssl"
					#安装OpenSSL
					cd $pwd/files
					tar xzf $openssl_version.tar.gz
					cd $openssl_version
					./config --prefix=$prefix/$openssl_version --openssldir=$prefix/$openssl_version/ssl -fPIC > /dev/null 2>&1
					if [ $? -eq 0 ];then
							make > /dev/null 2>&1
							make install > /dev/null 2>&1
					else
								echo -e "编译安装OpenSSL失败，五秒后自动退出脚本" "\033[31m Failure\033[0m"
								echo ""
								sleep 5
								exit
					fi

					if [ -e $prefix/$openssl_version/bin/openssl ];then
							echo "$prefix/$openssl_version/lib" >> /etc/ld.so.conf
							ldconfig
								echo -e "编译安装OpenSSL成功" "\033[32m Success\033[0m"
					fi
								echo ""	
						
}
# ------------升级open ssh -------------
function  upgradeOpenSSH(){
	# install openssh
			color blue "begin install openssh"
			cd $pwd/files
		#安装OpenSSH
				tar xzf $openssh_version.tar.gz  
				cd $openssh_version
				./configure --prefix=/usr --sysconfdir=/etc/ssh --with-ssl-dir=$prefix/$openssl_version --with-zlib=$prefix/$zlib_version --with-pam --with-md5-passwords > /dev/null 2>&1
				if [ $? -eq 0 ];then
					make > /dev/null 2>&1
					make install > /dev/null 2>&1
				else
					echo -e "编译安装OpenSSH失败，五秒后自动退出脚本" "\033[31m Failure\033[0m"
					echo ""
					sleep 5
					exit
				fi

				if [ -e /usr/sbin/sshd ];then
					echo -e "编译安装OpenSSH成功" "\033[32m Success\033[0m"
				fi
						echo ""
					
						#启动OpenSSH
			color blue "add sshd config"
			cp -rf $pwd/files/$openssh_version/contrib/redhat/sshd.init /etc/init.d/sshd
			cp -rf $pwd/files/$openssh_version/contrib/redhat/sshd.pam /etc/pam.d/sshd
			cp -rf $pwd/files/$openssh_version/contrib/ssh-copy-id  /usr/bin/
			
			chmod +x /etc/init.d/sshd
			chmod +x /usr/bin/ssh-copy-id
			chkconfig --add sshd
			sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
			
			chmod 600 /etc/ssh/ssh_host_rsa_key
			chmod 600 /etc/ssh/ssh_host_ecdsa_key
			chmod 600 /etc/ssh/ssh_host_ed25519_key
			color blue "try to start sshd"
			service sshd start > /dev/null 2>&1
			color blue "检验启动状态"
			openssh_running_status=`ps aux | grep -w "/usr/sbin/sshd" | grep -v grep | wc -l`
			if [ "$openssh_running_status" != "0" ];then
				echo -e "启动OpenSSH服务成功" "\033[32m Success\033[0m"
				echo ""
				$prefix/$openssl_version/bin/openssl version
				echo ""
				ssh -V
				else
				echo -e "启动OpenSSH服务失败，五秒后自动退出脚本" "\033[31m Failure\033[0m"
				sleep 5
				exit
				fi
				echo ""
		
}

function  addToPath(){
    touch /etc/profile.d/openssh_path.sh
    grep -q "$openssh_version" /etc/profile.d/openssh_path.sh|| echo 'export PATH=/opt/$openssh_version/bin:/opt/$openssh_version/sbin:$PATH' >> /etc/profile.d/openssh_path.sh
	source /etc/profile.d/openssh_path.sh
}

